package com.paradise.phone.fragment;


import android.app.Fragment;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.Surface;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.paradise.api.FaceCall;
import com.paradise.api.FacePhone;
import com.paradise.api.FacephoneListener;
import com.paradise.phone.activity.TalkingAcitvity;

import org.linphone.mediastream.video.AndroidVideoWindowImpl;

public class VideoFragment extends Fragment implements View.OnClickListener,
        View.OnTouchListener {

    private View rootView;

    private SurfaceView svRendingVideo;
    private SurfaceView svPreviewVideo;
    private ImageView ivSwitchCamera;
    private FrameLayout flPreviewVideo;

    private ImageView ivInAudio;

    private GestureDetector mGestureDetector;
    private float mZoomFactor = 1.f;
    private float mZoomCenterX, mZoomCenterY;

    private TalkingAcitvity talkingAcitvity;

    public VideoFragment() {
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(com.paradise.phone.R.layout.fragment_video, container, false);

        initView();

        rotaionCamera();

        initEvent();

        return rootView;
    }

    private void initEvent() {
        ivSwitchCamera.setOnClickListener(this);
    }

    private void initView() {

        svRendingVideo = (SurfaceView) rootView.findViewById(com.paradise.phone.R.id.gl_rending_view);
        svPreviewVideo = (SurfaceView) rootView.findViewById(com.paradise.phone.R.id.sv_preview_view);
        ivInAudio = (ImageView) rootView.findViewById(com.paradise.phone.R.id.iv_audio);
        ivSwitchCamera = (ImageView) rootView.findViewById(com.paradise.phone.R.id.iv_switch_camera);
        flPreviewVideo = (FrameLayout) rootView.findViewById(com.paradise.phone.R.id.fl_preview_view);

        talkingAcitvity = (TalkingAcitvity)getActivity();

        ((TalkingAcitvity) getActivity()).callbackFragment(svPreviewVideo,
                svRendingVideo, ivInAudio, ivSwitchCamera, flPreviewVideo);

        FaceCall.instance().displayVideo(svRendingVideo, svPreviewVideo, new FacephoneListener.FacephoneVideoStateListener() {
            @Override
            public void onVideoRenderingSurfaceReady(AndroidVideoWindowImpl androidVideoWindow, SurfaceView surfaceView) {

            }

            @Override
            public void onVideoRenderingSurfaceDestroyed(AndroidVideoWindowImpl androidVideoWindow) {
                ivSwitchCamera.setVisibility(View.INVISIBLE);
            }

            @Override
            public void onVideoPreviewSurfaceReady(AndroidVideoWindowImpl androidVideoWindow, SurfaceView surfaceView) {
                ivSwitchCamera.setVisibility(View.VISIBLE);
            }

            @Override
            public void onVideoPreviewSurfaceDestroyed(AndroidVideoWindowImpl androidVideoWindow) {

            }
        });

        if (getArguments() != null) {
            if (getArguments().getString("INCOMMING_CALL_TYPE").equals("audio")) {
                svPreviewVideo.setVisibility(View.INVISIBLE);
                svRendingVideo.setVisibility(View.INVISIBLE);
            }
        }

        mGestureDetector = new GestureDetector(getActivity(), new SimpleOnGestureListener(){
            @Override
            public boolean onDoubleTap(MotionEvent e) {
                //Log.d("flag->", "VideoFragment.java(onDoubleTap:114)->"+mZoomCenterX+","+mZoomCenterY);
                if (mZoomFactor == 1.f) {
                    // Zoom to make the video fill the screen vertically
                    float portraitZoomFactor = ((float) svRendingVideo.getHeight()) / (float) ((3 * svRendingVideo.getWidth()) / 4);
                    // Zoom to make the video fill the screen horizontally
                    float landscapeZoomFactor = ((float) svRendingVideo.getWidth()) / (float) ((3 * svRendingVideo.getHeight()) / 4);

                    mZoomFactor = Math.max(portraitZoomFactor, landscapeZoomFactor);
                }
                else {
                    resetZoom();
                }

                FacePhone.instance().getmLc()
                        .getCurrentCall().zoomVideo(mZoomFactor, mZoomCenterX, mZoomCenterY);

                return true;
            }

            @Override
            public boolean onSingleTapConfirmed(MotionEvent e) {
                talkingAcitvity.showBannerByFragment();

                return true;
            }

            @Override
            public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {

                if (mZoomFactor > 1) {
                    // Video is zoomed, slide is used to change center of zoom
                    if (distanceX > 0 && mZoomCenterX < 1) {
                        mZoomCenterX += 0.02;
                    } else if (distanceX < 0 && mZoomCenterX > 0) {
                        mZoomCenterX -= 0.02;
                    }
                    if (distanceY < 0 && mZoomCenterY < 1) {
                        mZoomCenterY += 0.02;
                    } else if (distanceY > 0 && mZoomCenterY > 0) {
                        mZoomCenterY -= 0.02;
                    }

                    /*if (mZoomCenterX > 1)
                        mZoomCenterX = 1;
                    if (mZoomCenterX < 0)
                        mZoomCenterX = 0;
                    if (mZoomCenterY > 1)
                        mZoomCenterY = 1;
                    if (mZoomCenterY < 0)
                        mZoomCenterY = 0;*/

                    //Log.d("flag->", "VideoFragment.java(onScroll:142)->x = "+mZoomCenterX+", y = "+mZoomCenterY);

                    FacePhone.instance().getmLc()
                            .getCurrentCall().zoomVideo(mZoomFactor, mZoomCenterX, mZoomCenterY);

                }
                return true;
            }
        });
        svRendingVideo.setOnTouchListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case com.paradise.phone.R.id.iv_switch_camera:
                FaceCall.instance().switchCamera(svPreviewVideo);
                break;
        }
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        rotaionCamera();
    }

    private int dip2px(float dpValue) {

        float scale = getResources().getDisplayMetrics().density;

        return (int) (dpValue * scale + 0.5f);
    }

    public SurfaceView getSvPreviewVideo() {
        return svPreviewVideo;
    }

    public SurfaceView getSvRendingVideo() {
        return svRendingVideo;
    }

    private void rotaionCamera() {
        int phoneDegree = 0;
        //获得手机方向
        int phoneRotate = getActivity().getWindowManager().getDefaultDisplay().getOrientation();
        //得到手机的角度
        switch (phoneRotate) {
            case Surface.ROTATION_0:
                phoneDegree = 0;
                break;        //0
            case Surface.ROTATION_90:
                phoneDegree = 90;
                break;      //90
            case Surface.ROTATION_180:
                phoneDegree = 180;
                break;    //180
            case Surface.ROTATION_270:
                phoneDegree = 270;
                break;    //270
        }

        if (phoneDegree == 270 || phoneDegree == 90) {
            FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) svPreviewVideo.getLayoutParams();
            lp.width = dip2px(160);
            lp.height = dip2px(120);
            svPreviewVideo.setLayoutParams(lp);
        } else {
            FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) svPreviewVideo.getLayoutParams();
            lp.width = dip2px(120);
            lp.height = dip2px(160);
            svPreviewVideo.setLayoutParams(lp);
        }
    }

    private void resetZoom() {
        mZoomFactor = 1.f;
        mZoomCenterX = mZoomCenterY = 0.5f;
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        mGestureDetector.onTouchEvent(event);
        return true;
    }
}
